(function( $ ){
  $(document).ready(function(){
    var options = {
      // table settings, only read during initialization
      rows: 8, // more rows = more processing cycles
      cols: 12, // more cols = more processing cycles
      spacing: 4, // table's cellspacing attribute
      padding: 0, // table's cellpadding attribute
      border: 0, // table's border attribute
      height: '40px', // css height property for each cell
      width: '40px', // css height property for each cell
      html: '', // each cell can contain some html
      // timer settings
      shift: 0, // any positive integer
      delay: 100, // milliseconds between frame refresh
      overlay_delay: 1000, // overlay animation speed milliseconds
      scroll_delay: 100, // scroll speed milliseconds
      // plasma settings
      plasma_type: 'hsv', // so far, 'hsv' or 'rgb', more to come
      blur: 2, // less than 1 sharpens the plasma, greater than 1 blurs
      r: 1, // red intensity 0-1, applies to plasma_type 'rgb'
      g: 1, // green intensity 0-1, applies to plasma_type 'rgb'
      b: 1, // blue intensity 0-1, applies to plasma_type 'rgb'
      h: 255, // hue multiplier, applies to plasma_type 'hsv'
      s: 1, // saturation intensity 0-1, applies to plasma_type 'hsv'
      v: 1, // value intensity 0-1, applies to plasma_type 'hsv'
      // overlay settings
      overlay: false, // boolean, on or off
      overlay_anim: false, // boolean true = animated, false = static
      overlay_loop: true, // boolean, true = loop, false = play once
      overlay_color: [255, 255, 255], // array of rgb values 0-255
      overlay_bias: [0,0],  // x,y cells to move overlay 
      overlay_data: [] // array for overlay pattern
    };

    $('#plasma').plasma(options);
    var plasma_data = $('#plasma').data('plasma');
    $('#delay').val(plasma_data.delay);
    $('#overlay_delay').val(plasma_data.overlay_delay);
    if (plasma_data.overlay_loop === true) {
      $('#overlay_loop').attr('checked', 'checked');
    }
    $('#scroll_delay').val(plasma_data.scroll_delay);
    $('#plasma_type option').each(function(){
      if (this.value == plasma_data.plasma_type) {
        $(this).attr('selected', 'selected');
      } else {
        $(this).removeAttr('selected');
      }
    });
    if ($.isArray(plasma_data.overlay_color)) {
      $('#overlay_color').val(plasma_data.overlay_color.join(','));
    } else {
      $('#overlay_color').val(plasma_data.overlay_color);
    }
    if ($.isArray(plasma_data.overlay_bias)) {
      $('#overlay_bias').val(plasma_data.overlay_bias.join(','));
    }
    $('#blur').val(plasma_data.blur);
    $('#r').val(plasma_data.r);
    $('#g').val(plasma_data.g);
    $('#b').val(plasma_data.b);
    $('#h').val(plasma_data.h);
    $('#s').val(plasma_data.s);
    $('#v').val(plasma_data.v);

    $('#stop').click(function(){
      $('#plasma').plasma('stop');
      return false;
    });

    $('#start').click(function(){
      $('#plasma').plasma('start');
      return false;
    });
    
    $('#delay').change(function(){
      $.extend($('#plasma').data('plasma'), {delay: this.value});
      return false;
    });

    $('#destroy').click(function(){
      if ($(this).text() == 'Destroy') {
        $('#plasma').plasma('destroy');
        $(this).text('Create');
      } else {
        $('#plasma').plasma(options);
        $(this).text('Destroy');
      }
      return false;
    });
    
    $('#plasma_type').change(function(){
      $.extend($('#plasma').data('plasma'), {plasma_type: $(this).val()});
      return false;
    });
    
    $('#r, #g, #b, #h, #s, #v').change(function(){
      $.extend($('#plasma').data('plasma'), {
        r: $('#r').val(),
        g: $('#g').val(),
        b: $('#b').val(),
        h: $('#h').val(),
        s: $('#s').val(),
        v: $('#v').val(),
      });
      return false;
    });

    $('#overlay').click(function(){
      var data = $('#plasma').data('plasma');
      var pattern = [
        [0,0,1,0,0,0,0,0,1,0,0,0],
        [0,0,0,1,0,0,0,1,0,0,0,0],
        [0,0,1,1,1,1,1,1,1,0,0,0],
        [0,1,1,0,1,1,1,0,1,1,0,0],
        [1,1,1,1,1,1,1,1,1,1,1,0],
        [1,0,1,1,1,1,1,1,1,0,1,0],
        [1,0,1,0,0,0,0,0,1,0,1,0],
        [0,0,0,1,1,0,1,1,0,0,0,0]
      ];
      if (data.overlay === true) {
        $('#plasma').plasma('hide_overlay');
      } else {
        $('#plasma').plasma('show_overlay', pattern);
        $.extend($('#plasma').data('plasma'), data);
      }
      return false;
    });

    $('#overlay_anim').click(function(){
      var data = $('#plasma').data('plasma');
      var pattern = [
        [
          [0,0,0,1,1,0,0,0],
          [0,0,1,1,1,1,0,0],
          [0,1,1,1,1,1,1,0],
          [1,1,0,1,1,0,1,1],
          [1,1,1,1,1,1,1,1],
          [0,0,1,0,0,1,0,0],
          [0,1,0,1,1,0,1,0],
          [1,0,1,0,0,1,0,1]
        ],[
          [0,0,0,1,1,0,0,0],
          [0,0,1,1,1,1,0,0],
          [0,1,1,1,1,1,1,0],
          [1,1,0,1,1,0,1,1],
          [1,1,1,1,1,1,1,1],
          [0,1,0,1,1,0,1,0],
          [1,0,0,0,0,0,0,1],
          [0,1,0,0,0,0,1,0]
        ]
      ];
      //var pattern = 'The jQuery Plasma Plug-in';
      if (data.overlay === true) {
        $('#plasma').plasma('hide_overlay');
      } else {
        data.overlay_anim = true;
        $('#plasma').plasma('show_overlay', pattern);
        $.extend($('#plasma').data('plasma'), data);
      }
      return false;
    });

    $('#overlay_text').click(function(){
      var data = $('#plasma').data('plasma');
      var pattern = $('#overlay_data').val();
      if (data.overlay === true) {
        $('#plasma').plasma('hide_overlay');
      } else {
        $('#plasma').plasma('show_overlay', pattern);
      }
      return false;
    });
    
    $('#overlay_bias').change(function(){
      if (this.value.match(/^(\d){1,3},(\d){1,3}$/)) {
        var value = this.value.split(/\s*,\s*/);
        $.extend($('#plasma').data('plasma'), {overlay_bias: value});
      }
      return false;
    });
    
    $('#overlay_color').change(function(){
      if (this.value.match(/^(\d){1,3},(\d){1,3},(\d){1,3}$/)) {
        var value = this.value.split(/\s*,\s*/);
      } else {
        var value = this.value;
      }
      $.extend($('#plasma').data('plasma'), {overlay_color: value});
      //alert($('#plasma').data('plasma').overlay_color);
      return false;
    });
    
    $('#overlay_loop').change(function(){
      var value = $(this).is(':checked') ? true : false;
      $.extend($('#plasma').data('plasma'), {overlay_loop: value, overlay_timeout: -1});
      return false;
    });
  });
})( jQuery );
